import background
import char
import config
import creator
import fps
from PySide6.QtCore import QTimer, QRect
from PySide6.QtGui import QPixmap, QPainter, Qt
from PySide6.QtWidgets import QMainWindow, QMessageBox
from Ui_MainWindow import Ui_MainWindow


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.timer = QTimer()
        self.screen = background.BackGround()
        self.snapshot = QPixmap()
        self.sheep = None
        self.wolf_creator = None
        self.start = False
        self.score = 0
        self.message_box = False
        self.fps = fps.FpsCalc()

        self.timer.timeout.connect(self.frame_update)
        self.ui.start.clicked.connect(self.start_game)
        self.setFixedSize(self.screen.width+config.WIDTH_LEFT, self.screen.height)
        self.timer.start(1000//config.FPS_SPEED)
        self.setMouseTracking(True)
        self.centralWidget().setMouseTracking(True)

    def frame_update(self):
        """更新每一帧"""
        self.update_snapshot()
        self.fps.add()
        if self.start:
            self.sheep.update()
            self.score += config.SC_KEY if self.sheep.key_ctrl else config.SC_MOUSE
            if self.wolf_creator.update(self.sheep):
                self.start = False
                self.message_box = True
                self.ui.op.setEnabled(True)

    def update_snapshot(self):
        """绘制该帧的图片"""
        new_snap = QPixmap(self.screen.width, self.screen.height)
        self.screen.draw(new_snap)
        if self.start:
            self.wolf_creator.draw(new_snap)
            self.sheep.draw(new_snap)
        else:
            pa = QPainter(new_snap)
            scrn_rect = QRect(0, 0, self.screen.width, self.screen.height)
            pa.setPen(config.TIP_COLOR)
            pa.setFont(config.TIP_FONT)
            pa.drawText(scrn_rect, Qt.AlignmentFlag.AlignCenter, config.TIP_TEXT)
        self.ui.score.setText(f'分数：{self.score}')
        self.ui.fps.setText(f'帧率：{round(self.fps.fps, config.FPS_PRECISION)}')
        self.snapshot = new_snap
        self.update()
        if self.message_box:
            QMessageBox.information(self, '游戏结束', self.ui.score.text())
            self.message_box = False

    def paintEvent(self, event):
        pa = QPainter(self)
        pa.drawPixmap(0, 0, self.snapshot)

    def mouseMoveEvent(self, event):
        if self.start:
            self.sheep.mouse_move(event)

    def start_game(self):
        key = self.ui.op.currentText() == '键盘'
        self.sheep = char.Sheep(key, self.screen)
        self.wolf_creator = creator.WolfCreator(self.screen)
        self.score = 0
        self.start = True
        self.ui.op.setEnabled(False)
        self.ui.start.setText('重新开始')

    def keyPressEvent(self, event):
        if self.start:
            self.sheep.key_press(event)

    def keyReleaseEvent(self, event):
        if self.start:
            self.sheep.key_release(event)
